package com.springboot.helloworld.service;

import com.springboot.helloworld.entity.Girl;
import com.springboot.helloworld.enums.ResultEnum;
import com.springboot.helloworld.exception.GirlException;
import com.springboot.helloworld.repository.GirlRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
* @description: 女生 service
* @author: xiaoliang.liu
* @date: 2018/7/2 0:29
* @version v1.0
*/
@Slf4j
@Service
public class GirlService {

    /** 女生 dao 接口 */
    private final GirlRepository girlRepository;

    @Autowired
    public GirlService(GirlRepository girlRepository) {
        this.girlRepository = girlRepository;
    }

    /**
     * 获得年龄
     * @return Integer
     */
    public Integer getAge(int id) {
        Girl girl = girlRepository.getOne(id);
        Integer age = girl.getAge();

        if (age < 10) {
            throw new GirlException(ResultEnum.LESS_TEN.getCode(), ResultEnum.LESS_TEN.getMsg());
        } else if (age > 10 && age < 16) {
            throw new GirlException(ResultEnum.TEN_TO_SIXTEEN.getCode(), ResultEnum.TEN_TO_SIXTEEN.getMsg());
        }

        return 1;
    }

    /**
     * 根据id查询一个女生的信息
     * @param id 女生id
     * @return Girl
     */
    public Girl getGirlById(Integer id) {
        Optional<Girl> girl = girlRepository.findById(id);
        return girl.orElse(null);
    }
}
